<?php
Class PdoDb
{ 
 protected $pdo;
 protected $res;
 protected $config;
 public function __construct()
 {
    if(file_exists("./config.php"))
    require_once("./config.php");
    else    
    echo "<script>window.location.href='../login.php'</script>";
    echo $pdoConfig['username'];
  }
 public function connect()
 {    
  if(file_exists("./config.php"))
  require_once("./config.php");
  else    
  echo "<script>window.location.href='../login.php'</script>";
  echo $padoConfig['username'];
  $this->pdo = new PDO($pdoConfig['dsn'],$pdoConfig['username'],$pdoConfig['pwd']);
  $this->pdo->query('set names utf8;');
  $this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  }
 public function close()
 {
    $this->pdo = null;
  }
 public function query($sql)
 {
    $res = $this->pdo->query($sql);
    if($res)
    $this->res = $res;
  }
 public function exec($sql)
 {
    $res = $this->pdo->exec($sql);
    if($res)
    {
      $this->res = $res;
    }
  }
 public function fetchAll()
 {
    $this->res->fetchAll();
  }
 public function fetch()
 {
    return $this->res->fetch();
  }
 public function fetchColumn()
 {
    return $this->res->fetchColumn();
  }
 public function lastInsertId()
 {
    return $this->res->lastInsertId();
  }
 public function select($debug,$mode,$table,$field="*",$sqlwhere="",$orderby="")
 {
    if(is_array($table))
    {
      $table = implode(', ',$table);
    }
    if(is_array($fields))
    {
      $fields = implode(', ',$fields);
    }
    if(is_array($sqlwhere))
    $sqlwhere = ' and'.implode(' and',$sqlwhere);
    if($debug === 0)
    {
      //返回记录数目
      if($mode === 2)
      {
        $this->query("select count(*) from $table where 1=1 $sqlwhere");
        $return = $this->fecthColumn();
      }
      //返回一条数据
      elseif ($mode === 1)
      {
        $this->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
        $return = $this->fetch();
      }
      else
      {
        $this->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
        $return = $this->fetchAll();
      }
      return;
    }
    else
    {
      if($mode === 2)
      {
        echo "select count(*) from $table where 1=1 $sqlwhere";
      }
      elseif ($mode === 1)
      echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
      else 
      echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
      if($debug === 2 )
      {
        exit;
      }
    }
  }
 
 /**
  *新增记录
  *int $debug 调试功能，是否输出sql语句
  *int $mode 返回类型  
  *string/array $table 表
  *string/array $set 要插入的字段
  */
 public function insert($debug=0,$mode,$table,$set)
 {
    if(is_array($table))
    {
      $talbe = implode(', ',$table);
    }
    if(is_array($set))
    {
      $set = implode(', ',$set);
    }
    if($debug === 0)
    {
      if($mode === 2)
      {
        $this->query("insert into $table set $set");
        $return = $this->lastInsertId();
      }
      elseif ($mode === 1)
      {
        $this->res = $this->exec("insert into $table set $set");
        $return = $this->res;
      }
      else
      {
        $this->query("insert into $table set $set");
        $return = NULL;
      }
      return $return;
    }
    else
    {
      echo "insert into $table set $set";
      //输出sql后结束
      if($debug === 2)exit;
    }
  }
 /**
  *update 
  *int $debug 调试功能，是否输出sql
  *int $mode 返回类型
  *string $table data table
  *string/array $set the data fields to be updated
  *string/array $sqlwhere the condition
  */
 public function update($debug,$mode,$table,$set,$sqlwhere="")
 {
    if(is_array($table))
    {
      $table = implode(', ',$table);
    }
    if(is_array($set)) $set = implde(', ',$set);
    if(is_array($sqlwhere)) $sqlwhere = ' and '.implode(' and ',$sqlwhere);
    if($debug === 0)
    {
      if($mode === 1)
      {
        $this->exec("update $table set $set where 1=1 $sqlwhere");
        $return = $this->res;
      }
      else
      {
        $this->query("update $table set $set where 1=1 $sqlwhere");
        $return = $this->res;
      }
      return $return;
    }
    else
    {
      echo "update $table set $set where 1=1 $sqlwhere";
      if($debug === 2) exit;
    }
  }
 /**
  *int $debug display sql
  * int $mode the type of return
  *string $table data table
  *string/array $sqlwhere condition
  */
 public function delete($debug,$mode,$table,$sqlwhere="")
 {
    if(is_array($sqlwhere)) $sqlwhere = ' and '.implode(' and ',$sqlwhere);
    if($debug === 0 )
    {
      if($mode === 1)
      {
        $this->exec("delete from $table where 1=1 $sqlwhere");
        $return = $this->res;
      }
      else
      {
        $this->query("delete from $table where 1=1 $sqlwhere");
        $return = NULL;
      }
      return $return;
    }
    else
    {
      echo "delete from $table where 1=1 $sqlwhere";
      if($debug === 2)exit;
    }
  }
}
new PdoDb();
?>